package com.whxy02.tolearn.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whxy02.tolearn.app.beans.QuestionType;
import com.whxy02.tolearn.app.dao.QuestionTypeDao;
import com.whxy02.tolearn.app.service.QuestionTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class QuestionTypeServiceImpl implements QuestionTypeService {

    private QueryWrapper<QuestionType> wrapper=new QueryWrapper<>();
    @Resource
    QuestionTypeDao questionTypeDao;

    @Override
    public List<QuestionType> getAll() throws Exception {
        return questionTypeDao.selectList(null);
    }

    @Override
    public QuestionType getById(Integer id) throws Exception {
        return questionTypeDao.selectById(id);
    }

    @Override
    public int save(QuestionType questionType) throws Exception {
        return questionTypeDao.insert(questionType);
    }

    @Override
    public int removeById(Integer id) throws Exception {
        return questionTypeDao.deleteById(id);
    }

    @Override
    public int change(QuestionType questionType) throws Exception {
        return questionTypeDao.updateById(questionType);
    }

    @Override
    public List<QuestionType> getBySubject(Integer subjectId) throws Exception {
        wrapper.clear();
        wrapper.eq("subject_id",subjectId);
        return questionTypeDao.selectList(wrapper);
    }

    @Override
    public boolean notHasType(String typeName) throws Exception {
        wrapper.clear();
        wrapper.eq("type_name",typeName);
        return questionTypeDao.selectOne(wrapper)==null;
    }

    @Override
    public QuestionType getByTypeName(String typeName) throws Exception {
        wrapper.clear();
        wrapper.eq("type_name",typeName);
        return questionTypeDao.selectOne(wrapper);
    }

    @Override
    public QuestionType getByTypeAndSubject(String typeName, Integer subjectId) throws Exception {
        wrapper.clear();
        wrapper.eq("type_name",typeName).eq("subject_id",subjectId);
        return questionTypeDao.selectOne(wrapper);
    }
}
